<?php
declare (strict_types = 1);
namespace app\common\model\advert;

use app\common\logic\Attach;
use app\common\model\BaseModel;

/**
 * 定义广告图配置数据模型
 */
class AdvertLink extends BaseModel
{
    // 表名
    protected $name = 'advert_link';

    // 无需创建时间
    protected $createTime = false;

    // 填充字段
    protected $append = ['link_type_text', 'link_data_type_text', 'imageUrl'];

    // 定义表主键
    protected $pk = 'id';

    // 全局查询范围
    protected $globalScope = ['hidden'];

    public function scopeHidden($query)
    {
        $query->hidden(['id','advert_id','image','sort','update_time']);
    }

    // 文字跳转类型
    public function getLinkTypeTextAttr($value, $data)
    {
        $text = [
            0 => '纯图片',
            1 => '内容',
            2 => '链接',
        ];

        return $text[$data['link_type']];
    }

    // 设置类型
    public function setLinkDataTypeAttr($value, $data)
    {
        $type = '';
        switch ($value) {
            case '1':
                $type = 'video';
                break;
            case '2':
                $type = 'live';
                break;
            case '3':
                $type = 'live_small';
                break;
            default:
                $type = 'public';
                break;
        }

        return $type;
    }

    // 跳转的内容数据名称
    public function getLinkDataTypeTextAttr($value, $data)
    {
        $text = [
            'video'      => '点播',
            'live'       => '大班课',
            'live_small' => '小班课',
            'public'     => '公共',
        ];

        return $data['link_type'] == 1 ? $text[$data['link_data_type']] : '';
    }

    // 图片URL
    public function imageUrl()
    {
        return $this->belongsTo(Attach::class, 'image', 'id')->bind(['image_url' => 'fileurl']);
    }

}
